import {message, Modal} from "ant-design-vue";
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import "./stylesheet/commonModal.less";
import {createVNode } from 'vue';
/**
 * 调用c#异步函数方法超时
 * @param fun
 * @param time
 * @returns
 */
export function callTimeout(fun: Function, time: number) {
  return (...args: any[]) => {
    return Promise.race([
      fun(...args),
      new Promise((resolve) => {
        setTimeout(function(){
          resolve(true);
          message.error("操作超时了！")
        }, time);
      }),
    ]);
  };
}

  /**
   *  [按键事件广播][向父级或者节点进行事件广播]
   */
export function broadcastChildMessage(type: string,data: any ) {
      console.log("向子级iframe事件广播=>",type,data)
      const iframes: NodeListOf<HTMLIFrameElement> = document.querySelectorAll("iframe");
      iframes.forEach(iframe => {
         if(iframe)iframe.contentWindow?.postMessage({
              type,
              data
          }, "*")
      })
  }

  export function enumToArray(enumObject){
    const all: any[] = [];
    for(const key in enumObject){
      all.push(enumObject[key]);
    }
    return all;
  }


/**
 *
 * @param param0 4种弹出框样式 info warning danger success
 * @returns
 */
interface IAlertMessageProps{
  type: string;
  msg: string;
  title?: string;
  onOk?: () => any,
  onCancel?: () => any,
}

export function AlertMessage({
                               type,msg,title,onOk,onCancel
                             }: IAlertMessageProps) {
  return new Promise((resolve) => {
    if(!['info','success','danger','warning'].includes(type)){
      throw new Error("AlertMessage 只支持4种弹出框样式 info warning danger success");
    }
    const modalType = type==="danger"?"error":type;
    if(Modal[modalType])Modal[modalType]({
      title: title,
      className: `ydt-common-modal-${type}-wrapper`,
      content: <p v-html={msg.replace(/(\d+)/g, `<span class='ant-modal-confirm-content-number'>$1</span>`)}></p>,
      keyboard: true,
      okButtonProps:{
        htmlType: "submit"
      },
      transitionName:"",
      onOk() {
        if(onOk)onOk();
        resolve(true);
      },
      onCancel() {
        if(onCancel)onCancel();
        resolve(true);
      },
    });
  });
}

interface IConfirmBeforeProps{
  title: string;
  msg: string;
  onOk?: () => any,
  onCancel?: () => any,
}
export function ConfirmBefore (props:IConfirmBeforeProps){
  Modal.confirm({
    title: props.title,
    content: props.msg,
    okText: props.onOk,
    cancelText: props.onCancel,
  });
}



